/**
 * Created by Administrator on 2017/6/30 0030.
 */
"use strict";
angular.module('app.sensor').controller('AlertCtrl', function ($scope,$cookieStore, $http) {
    $scope.userid = $cookieStore.get('userid');
    $scope.fid = $cookieStore.get('userinfo').fid;
    $scope.normalUser = false;
    if ($scope.fid !== "0" && $scope.fid !== "1") {
        $scope.normalUser = true;
    }

    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 40, 50],
        deviceid:'',
        timefrom:'',
        timeto:'',
        handle_status:'',
        onChange: function () {
        }
    };
    $scope.poc = {};
    $scope.typof = {};
    // 获取通知类型
    $http.get(baseurl+'/public_types/notice_type').then(function (res) {
        if(res.data.ret === 1){
            $scope.call = res.data.info;
            var arr = res.data.info;
            for (var q = 0;q < arr.length; q++ ){
                var obj = arr[q];
                $scope.typof[obj.id] = obj.name;
            }
            $scope.first_notice_type = res.data.info[0].id;
        }
    });
    // 报警类型
     $http.get(baseurl+'/public_types/warn_type').then(function (res) {
        if(res.data.ret === 1){
            $scope.caveat = res.data.info;
            var arr = res.data.info;
            for (var q = 0;q < arr.length; q++ ){
                var obj = arr[q];
                $scope.poc[obj.id] = obj.name;
            }
        }
     });
    $scope.sends = function () {
       var page_obj = {
            page:$scope.paginationConf.currentPage,
            rows:$scope.paginationConf.itemsPerPage     
        }
        var did = $scope.paginationConf.deviceid;
        var tid = $scope.paginationConf.timefrom;
        var toid = $scope.paginationConf.timeto;
        var status = $scope.paginationConf.handle_status;
        var query = '?deviceid='+did+'&handle_status=='+status+'&time='+tid+','+toid;
        $http.post(baseurl + '/list/select/view_warnreport_project' + query, page_obj).then(function (res) {
  
            if (res.data.ret = 1 && res.data.rows != null) {
                var arr = res.data.rows; 
                for (var i = 0; i < arr.length; i++) {
                    var obj = arr[i];
                    var time = obj.time;
                    obj.warn_type_name = $scope.poc[obj.warn_type];
                    obj.notice_type_name = $scope.typof[obj.handle_status];
                    obj.time = moment(time).format("YYYY-MM-DD HH:mm:ss");
                    if (obj.time === 'Invalid date') {
                            obj.time = time;
                    }
                }
                $scope.datas = arr;
                $scope.paginationConf.totalItems = res.data.records;               
            }
        })
    }
    //$scope.protocol_table = 'log_' + $location.search().port;
    //获取字段中文
    function getdata() {
        $http.get(baseurl + '/table_columns/' + $scope.protocol_table).then(function (res) {
            if (res.data.ret == 1) {
                $scope.datas = res.data.info;
                for (var i = 0; i < $scope.datas.length; i++) {
                    $scope.datas[i].status = false;
                    if ($scope.datas[i].COLUMN_NAME == 'id' || $scope.datas[i].COLUMN_NAME == 'fid' || $scope.datas[i].COLUMN_NAME == 'projectid' || $scope.datas[i].COLUMN_NAME == 'deviceid' || $scope.datas[i].COLUMN_NAME == 'time') {
                        $scope.datas[i].status = true;
                    }
                }
            }
        });
    }
    getdata();
    $scope.editdata = {};
    $scope.showEditAlert = function (data) {
        $scope.editid = data.id;
        $scope.editdata.memo =  data.memo;
        $('#editAlert').modal('show');
    }

    $scope.updatedata = function () {
        $http.put(baseurl+'/data/warn_report?id=='+$scope.editid,$scope.editdata).then(function(res){
            // console.log(res.data);
            if (res.data.ret == 1) {
                 $("#editAlert").modal("hide");
                 $scope.sends();
            }
        });

    }

    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.sends);

});